package mn.more.wits.client;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import mn.more.wits.client.dto.ExamListDTO;
import mn.more.wits.client.dto.ExamQuestionAttachmentDTO;
import mn.more.wits.client.dto.ExamQuestionDTO;
import mn.more.wits.client.dto.ExamSessionDTO;
import mn.more.wits.client.dto.ExamSummaryDTO;
import mn.more.wits.client.dto.UserDTO;
import mn.more.wits.client.gui.WitsTable;
import mn.more.wits.client.util.FormatUtil;
import net.mygwt.ui.client.Events;
import net.mygwt.ui.client.Style;
import net.mygwt.ui.client.event.BaseEvent;
import net.mygwt.ui.client.event.Listener;
import net.mygwt.ui.client.widget.Button;
import net.mygwt.ui.client.widget.ContentPanel;
import net.mygwt.ui.client.widget.Viewport;
import net.mygwt.ui.client.widget.layout.BorderLayoutData;
import net.mygwt.ui.client.widget.table.DateTimeCellRenderer;
import net.mygwt.ui.client.widget.table.NumberCellRenderer;
import net.mygwt.ui.client.widget.table.TableColumn;
import net.mygwt.ui.client.widget.table.TableItem;

/**
 * //todo -- testing only... gotta remove will be removed soon.  currently use for testing purposes (so that we don't
 * always need RPC for UI testing)
 *
 * @author <a href="mailto:mike.liu@aptechmongolia.edu.mn">Mike Liu</a>
 * @version $Revision: 119 $
 */
public final class DummyBootstrap {

	private static ExamSessionDTO session = initExamSession();
	private static ExamQuestionDTO[] exams = initExams();
	private static final int QUESTION_COUNT = 5;

	private DummyBootstrap() {}

	public static UserDTO getDefaultExamTaker() {
		UserDTO user = new UserDTO();
		user.setUserName("bataar1");
		user.setDisplayName("Bataar Оуман");
		user.setLoginDate(new Date());
		user.setMainRole("examTaker");
		AppContext.setUser(user);
		return user;
	}

	//public static void showExamTakerStartPage() {
	//	UserDTO user = getDefaultExamTaker();
	//
	//	FillLayout layout = new FillLayout();
	//	layout.setSpacing(10);
	//	MainPanel main = AppContext.getMainPanel();
	//	main.setLayout(layout);
	//
	//	ExamTakerStartPage startPage = new ExamTakerStartPage();
	//	main.add(startPage);
	//}

	private static ExamSessionDTO initExamSession() {
		ExamSessionDTO session = new ExamSessionDTO();
		session.setBackwardAllowed(true);
		session.setForwardWithoutAnswer(true);
		session.setMaxMinutes(180);
		session.setSubjectName("Core Java");
		session.setTestId("ECAS_JAV_FT_1");
		session.setTestName("Weekly Test #1");
		session.setTotalQuestions(QUESTION_COUNT);
		return session;
	}

	private static ExamQuestionDTO[] initExams() {
		exams = new ExamQuestionDTO[QUESTION_COUNT];

		ExamQuestionAttachmentDTO qAttach = new ExamQuestionAttachmentDTO();
		qAttach.setDisplayName("Code Snipplet #1");
		qAttach.setDisplayOrder(1);
		qAttach.setText("public class Compare\n" +
		                "{\n" +
		                "  public static void main(String args[])\n" +
		                "  {\n" +
		                "    int x=10, y=5;\n" +
		                "    if(x<10)\n" +
		                "      y=1;\n" +
		                "    if(x>=10) y*=2;\n" +
		                "    System.out.println(\"y is \" + y);\n" +
		                "  }\n" +
		                "}");
		qAttach.setType(ExamQuestionAttachmentDTO.TYPE_CODE_SNIPPET);

		exams[0] = new ExamQuestionDTO();
		exams[0].setAttachments(new ExamQuestionAttachmentDTO[]{qAttach});
		exams[0].setLast(false);
		exams[0].setMaxMinute(120);
		exams[0].setIndex(1);
		exams[0].setResponses(
			new String[]{"The program does not compile",
			             "The program compiles and prints y is 2 when executed",
			             "The program compiles and prints y is 5 when executed",
			             "The program compiles and prints y is 10 when executed"});
		exams[0].setText("What would happen when the following is compiled and executed?");
		exams[0].setType(ExamQuestionDTO.TYPE_MCQ);
		exams[0].setMcqMaxSelect(2);

		exams[1] = new ExamQuestionDTO();
		exams[1].setLast(false);
		exams[1].setMaxMinute(120);
		exams[1].setIndex(2);
		exams[1].setResponses(
			new String[]{"Java Certified Encryption",
			             "Java Cryptography Extension",
			             "Java Connected Extension",
			             "None of these"});
		exams[1].setText("JCE is ___________________.");
		exams[1].setType(ExamQuestionDTO.TYPE_MCQ);
		exams[1].setMcqMaxSelect(2);

		exams[2] = new ExamQuestionDTO();
		exams[2].setLast(false);
		exams[2].setMaxMinute(120);
		exams[2].setIndex(3);
		exams[2].setResponses(
			new String[]{"To verify the correctness of the byte code before executing it.",
			             "To make sure that the code is not loading system classes.",
			             "To load the correct Security Manager for the running code.",
			             "To restrict system access from the running code."});
		exams[2].setText("What is the job of the Java security manager?");
		exams[2].setType(ExamQuestionDTO.TYPE_MCQ);
		exams[2].setMcqMaxSelect(2);

		exams[3] = new ExamQuestionDTO();
		exams[3].setLast(false);
		exams[3].setMaxMinute(120);
		exams[3].setIndex(4);
		exams[3].setResponses(
			new String[]{"Indexed property",
			             "Action property",
			             "Constrained property",
			             "Event"});
		exams[3].setText("The JavaBean property that fires an event before its value is changed is a __________?");
		exams[3].setType(ExamQuestionDTO.TYPE_MCQ);
		exams[3].setMcqMaxSelect(2);

		exams[4] = new ExamQuestionDTO();
		exams[4].setLast(true);
		exams[4].setMaxMinute(120);
		exams[4].setIndex(5);
		exams[4].setResponses(
			new String[]{"Registry",
			             "LocateRegistry",
			             "Naming",
			             "Lookup"});
		exams[4].setText("Which class is used to register and lookup the remote object?");
		exams[4].setType(ExamQuestionDTO.TYPE_MCQ);
		exams[4].setMcqMaxSelect(2);

		return exams;
	}

	public static ExamSessionDTO getSampleData() { return session; }

	public static ExamQuestionDTO getSampleQuestion(int index) { return exams[index]; }

	public static ExamListDTO getSampleAvailableExamList() {
		List list = new ArrayList();
		ExamSummaryDTO s = new ExamSummaryDTO();
		list.add(s);
		s.setTestId("TEST_1");
		s.setTestName("Friday Test");
		s.setSubjectName("Core Java I");
		s.setAvailableSince(FormatUtil.toDateTime("2008.05.28 09:15:00"));
		s.setAvailableUntil(FormatUtil.toDateTime("2008.05.28 09:45:00"));
		s.setMaxMinutes(30);
		s.setTotalQuestions(12);

		s = new ExamSummaryDTO();
		list.add(s);
		s.setTestId("TEST_1");
		s.setTestName("Friday Test");
		s.setSubjectName("Core Java I");
		s.setAvailableSince(FormatUtil.toDateTime("2008.05.28 09:15:00"));
		s.setAvailableUntil(FormatUtil.toDateTime("2008.05.28 09:45:00"));
		s.setMaxMinutes(30);
		s.setTotalQuestions(12);

		s = new ExamSummaryDTO();
		list.add(s);
		s.setTestId("TEST_1");
		s.setTestName("Friday Test");
		s.setSubjectName("Core Java I");
		s.setAvailableSince(FormatUtil.toDateTime("2008.05.28 09:15:00"));
		s.setAvailableUntil(FormatUtil.toDateTime("2008.05.28 09:45:00"));
		s.setMaxMinutes(30);
		s.setTotalQuestions(12);

		s = new ExamSummaryDTO();
		list.add(s);
		s.setTestId("TEST_1");
		s.setTestName("Friday Test");
		s.setSubjectName("Core Java I");
		s.setAvailableSince(FormatUtil.toDateTime("2008.05.28 09:15:00"));
		s.setAvailableUntil(FormatUtil.toDateTime("2008.05.28 09:45:00"));
		s.setMaxMinutes(30);
		s.setTotalQuestions(12);

		s = new ExamSummaryDTO();
		list.add(s);
		s.setTestId("TEST_1");
		s.setTestName("Friday Test");
		s.setSubjectName("Core Java I");
		s.setAvailableSince(FormatUtil.toDateTime("2008.05.28 09:15:00"));
		s.setAvailableUntil(FormatUtil.toDateTime("2008.05.28 09:45:00"));
		s.setMaxMinutes(30);
		s.setTotalQuestions(12);

		s = new ExamSummaryDTO();
		list.add(s);
		s.setTestId("TEST_1");
		s.setTestName("Friday Test");
		s.setSubjectName("Core Java I");
		s.setAvailableSince(FormatUtil.toDateTime("2008.05.28 09:15:00"));
		s.setAvailableUntil(FormatUtil.toDateTime("2008.05.28 09:45:00"));
		s.setMaxMinutes(30);
		s.setTotalQuestions(12);

		s = new ExamSummaryDTO();
		list.add(s);
		s.setTestId("TEST_1");
		s.setTestName("Friday Test");
		s.setSubjectName("Core Java I");
		s.setAvailableSince(FormatUtil.toDateTime("2008.05.28 09:15:00"));
		s.setAvailableUntil(FormatUtil.toDateTime("2008.05.28 09:45:00"));
		s.setMaxMinutes(30);
		s.setTotalQuestions(12);

		s = new ExamSummaryDTO();
		list.add(s);
		s.setTestId("TEST_1");
		s.setTestName("Friday Test");
		s.setSubjectName("Core Java I");
		s.setAvailableSince(FormatUtil.toDateTime("2008.05.28 09:15:00"));
		s.setAvailableUntil(FormatUtil.toDateTime("2008.05.28 09:45:00"));
		s.setMaxMinutes(30);
		s.setTotalQuestions(12);

		s = new ExamSummaryDTO();
		list.add(s);
		s.setTestId("TEST_1");
		s.setTestName("Friday Test");
		s.setSubjectName("Core Java I");
		s.setAvailableSince(FormatUtil.toDateTime("2008.05.28 09:15:00"));
		s.setAvailableUntil(FormatUtil.toDateTime("2008.05.28 09:45:00"));
		s.setMaxMinutes(30);
		s.setTotalQuestions(12);

		s = new ExamSummaryDTO();
		list.add(s);
		s.setTestId("TEST_1");
		s.setTestName("Friday Test");
		s.setSubjectName("Core Java I");
		s.setAvailableSince(FormatUtil.toDateTime("2008.05.28 09:15:00"));
		s.setAvailableUntil(FormatUtil.toDateTime("2008.05.28 09:45:00"));
		s.setMaxMinutes(30);
		s.setTotalQuestions(12);

		ExamSummaryDTO[] summaries = new ExamSummaryDTO[list.size()];
		for (int i = 0; i < summaries.length; i++) { summaries[i] = (ExamSummaryDTO) list.get(i); }

		ExamListDTO examListDTO = new ExamListDTO();
		examListDTO.setSummaries(summaries);
		return examListDTO;
	}

	public static void addBeginTestButton(ContentPanel panel) {
		panel.setPadding(10);
		Button tmpButton = new Button("start exam");
		tmpButton.addListener(Events.Select, new Listener() {

			public void handleEvent(BaseEvent be) {
				AppContext.showLoading();
				Viewport viewport = AppContext.getViewport();
				viewport.remove(AppContext.getMainPanel());
				viewport.add(new ExamPanel(),
				             new BorderLayoutData(Style.CENTER));
				viewport.layout(true);
				AppContext.hideLoading();
			}
		});

		panel.add(tmpButton);
	}

	public static void addAvailableExams(ContentPanel panel) {
		WitsTable availableExamTable = new WitsTable(new String[]{"Test Name",
		                                                          "Subject",
		                                                          "# of Questions",
		                                                          "Time Limit",
		                                                          "Available From",
		                                                          "Available Until",
		                                                          "Needed",
		                                                          "Team",
		                                                          "Comments"});
		ExamListDTO availableExams = getSampleAvailableExamList();
		ExamSummaryDTO[] summaries = availableExams.getSummaries();
		for (int i = 0; i < summaries.length; i++) {
			ExamSummaryDTO summary = summaries[i];
			TableItem item = new TableItem(new Object[]{summary.getTestName(),
			                                            summary.getSubjectName(),
			                                            new Integer(summary.getTotalQuestions()),
			                                            new Integer(summary.getMaxMinutes()),
			                                            summary.getAvailableSince(),
			                                            summary.getAvailableUntil(),
			                                            summary.getRequiredTools(),
			                                            new Boolean(summary.isTeamwork()),
			                                            summary.getComments()});
			item.setData("id", summary.getTestId());
			availableExamTable.add(item);
		}

		availableExamTable.disableTextSelection(true);
		availableExamTable.enableEvents(true);
		availableExamTable.sort(0, Style.ASC);
		// all columns are sortable and resizable
		for (int i = 0; i < availableExamTable.getColumnCount(); i++) {
			TableColumn column = availableExamTable.getColumnModel().getColumn(i);
			column.setSortable(true);
			column.setResizable(true);
		}

		TableColumn column = availableExamTable.getColumnModel().getColumn(2);
		column.setRenderer(new NumberCellRenderer("##"));
		column.setAlignment(Style.RIGHT);

		column = availableExamTable.getColumnModel().getColumn(3);
		column.setRenderer(new NumberCellRenderer("##"));
		column.setAlignment(Style.RIGHT);

		column = availableExamTable.getColumnModel().getColumn(4);
		column.setRenderer(new DateTimeCellRenderer(FormatUtil.LONG_DATE));
		column.setAlignment(Style.RIGHT);

		column = availableExamTable.getColumnModel().getColumn(5);
		column.setRenderer(new DateTimeCellRenderer(FormatUtil.LONG_DATE));
		column.setAlignment(Style.RIGHT);

		availableExamTable.addListener(Events.CellDoubleClick | Events.KeyPress, new Listener() {

			public void handleEvent(BaseEvent be) {
				AppContext.showLoading();
				Viewport viewport = AppContext.getViewport();
				viewport.remove(AppContext.getMainPanel());
				viewport.add(new ExamPanel(), new BorderLayoutData(Style.CENTER));
				viewport.layout(true);
				AppContext.hideLoading();
			}
		});

		panel.add(availableExamTable);
	}
}
